<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="电影名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入电影名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="分类" prop="cid">
        <el-select v-model="queryParams.cid" placeholder="请选择分类" clearable>
          <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-input v-model="queryParams.keyword" placeholder="请输入关键字" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="showTime">
        <el-date-picker clearable v-model="queryParams.showTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间" />
      </el-form-item>
      <el-form-item label="是否付费" prop="needPay">
        <el-select v-model="queryParams.needPay" placeholder="请选择" clearable>
          <el-option v-for="item in payOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:movies:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:movies:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:movies:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:movies:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moviesList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="电影ID" align="center" prop="id" />
      <el-table-column label="电影名称" align="center" prop="title" />
      <el-table-column label="电影图片" align="center" prop="picPath">
        <template #default="scope">
          <el-image v-if="scope.row.picPath" :src="scope.row.picPath" :preview-src-list="[scope.row.picPath]"
            fit="cover" class="table-image" lazy></el-image>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="cid" :formatter="categoryFormatter" />
      <el-table-column label="电影描述" align="center" prop="contentDesc" show-overflow-tooltip />
      <el-table-column label="关键字" align="center" prop="keyword" />
      <el-table-column label="序号" align="center" prop="orderNo" />
      <el-table-column label="创建时间" align="center" prop="showTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.showTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="导演" align="center" prop="director" />
      <el-table-column label="演员" align="center" prop="actors" show-overflow-tooltip />
      <el-table-column label="播放连接" align="center" prop="url" show-overflow-tooltip>
        <template #default="scope">
          <a :href="scope.row.url" target="_blank" v-if="scope.row.url" class="play-link" title="点击播放">
            <i class="el-icon-video-play play-icon"></i>
          </a >
        </template>
      </el-table-column>
      <el-table-column label="是否付费" align="center" prop="needPay" :formatter="payFormatter" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button  type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:movies:edit']">修改</el-button>
          <el-button  type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:movies:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="电影名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入电影名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类" prop="cid">
              <el-select v-model="form.cid" placeholder="请选择分类" clearable style="width: 100%">
                <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="序号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入序号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否付费" prop="needPay">
              <el-select v-model="form.needPay" placeholder="请选择" style="width: 100%">
                <el-option v-for="item in payOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="导演" prop="director">
              <el-input v-model="form.director" placeholder="请输入导演姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="showTime">
              <el-date-picker clearable v-model="form.showTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择创建时间" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键字,用来搜索" />
        </el-form-item>

        <el-form-item label="电影图片" prop="picPath">
          <el-input v-model="form.picPath" placeholder="请输入电影图片路径" />
        </el-form-item>

        <el-form-item label="播放连接" prop="url">
          <el-input v-model="form.url" type="textarea" placeholder="请输入播放链接" />
        </el-form-item>

        <el-form-item label="演员" prop="actors">
          <el-input v-model="form.actors" type="textarea" placeholder="请输入演员姓名" />
        </el-form-item>

        <el-form-item label="电影描述" prop="contentDesc">
          <el-input v-model="form.contentDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMovies, getMovies, delMovies, addMovies, updateMovies } from "@/api/system/movies"

export default {
  name: "Movies",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      moviesList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        picPath: null,
        cid: null,
        contentDesc: null,
        keyword: null,
        orderNo: null,
        showTime: null,
        director: null,
        actors: null,
        url: null,
        needPay: null
      },
      form: {},
      rules: {},
      categoryOptions: [
        { value: "0", label: "特色" },
        { value: "1", label: "高观看率" },
        { value: "2", label: "高评分" },
        { value: "3", label: "最新上映" }
      ],
      payOptions: [
        { value: "0", label: "不需要付费" },
        { value: "1", label: "需要付费" }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listMovies(this.queryParams)
        .then(response => {
          this.moviesList = response.rows
          this.total = response.total
        })
        .finally(() => {
          this.loading = false
        })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: null,
        title: null,
        picPath: null,
        cid: null,
        contentDesc: null,
        keyword: null,
        orderNo: null,
        showTime: null,
        director: null,
        actors: null,
        url: null,
        needPay: null
      }
      this.resetForm("form")
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加电影信息"
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMovies(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改电影信息"
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const request = this.form.id ? updateMovies(this.form) : addMovies(this.form)
          request.then(response => {
            this.$modal.msgSuccess(this.form.id ? "修改成功" : "新增成功")
            this.open = false
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm(`是否确认删除电影编号为"${ids}"的数据项？`)
        .then(() => delMovies(ids))
        .then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
        .catch(() => { })
    },
    handleExport() {
      this.download('system/movies/export', {
        ...this.queryParams
      }, `movies_${new Date().getTime()}.xlsx`)
    },
    categoryFormatter(row, column, cellValue) {
      const category = this.categoryOptions.find(item => item.value === String(cellValue))
      return category ? category.label : (cellValue || '未知分类')
    },
    payFormatter(row, column, cellValue) {
      const pay = this.payOptions.find(item => item.value === String(cellValue))
      return pay ? pay.label : (cellValue === '0' ? '不需要付费' : cellValue === '1' ? '需要付费' : '未知')
    }
  }
}
</script>

<style scoped>
.play-link {
  color: #606266;
  text-decoration: none;
  transition: color 0.3s;
}

.play-link:hover {
  color: #409EFF;
}

.play-link i {
  margin-right: 5px;
}

.table-image {
  width: 80px;
  height: 60px;
  border-radius: 4px;
  cursor: pointer;
}

.table-image:hover {
  transform: scale(1.05);
  transition: transform 0.3s;
}
.play-icon {
  font-size: 24px;
  color: #08f904;
  cursor: pointer;
  transition: all 0.3s;
}
</style>